package com.fzrskj.cloud.free.dic.service;

import com.fzrskj.cloud.free.consts.AppCode;
import com.fzrskj.cloud.free.core.enums.DicParamEnum;
import com.fzrskj.cloud.free.core.utils.EncryptHelper;
import com.fzrskj.cloud.free.core.utils.EncryptUtils;
import com.fzrskj.cloud.free.dic.bean.DicParam;
import com.fzrskj.cloud.free.dic.bean.DicParamIndex;
import com.fzrskj.cloud.free.dic.bean.DicParamItem;
import com.fzrskj.cloud.free.dic.bean.DicVipgrade;
import com.fzrskj.cloud.free.dic.utils.DicParamIndexUtils;
import com.fzrskj.cloud.free.dic.utils.DicParamItemUtils;
import com.fzrskj.cloud.free.dic.utils.DicParamUtils;
import com.fzrskj.cloud.free.dic.utils.DicVipgradeUtils;
import com.winiis.component.common.utils.other.LicenseUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;
import ms.core.tool.XmlCodeParser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class DicParamService extends CObject {
	/**
	 * 获取数据字典表
	 * @return
	 */
	public String getListDicParamIndex(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		DicParamIndexUtils dicParamIndexUtils = new DicParamIndexUtils();
		//1.获取数据字典
		List<DicParamIndex> dicParamIndexList = dicParamIndexUtils.getSysParamIndexList(0);
		String code = dicParamIndexUtils.getErrCode();
		//3.返回错误代码
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		//4.格式化list集合
		return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JsonTool.listBeanToJson(dicParamIndexList));
	}

	/**
	 * 添加数据字典表
	 * @param p={"paramCName":"paramCName","paramEName":"paramEName"}
	 * @param sid
	 * @return
	 */
	public String addDicParamIndex(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String paramCName = JsonTool.getStrValue(p, "paramCName", "");
		String paramEName = JsonTool.getStrValue(p, "paramEName", "");
		DicParamIndexUtils dicParamIndexUtils = new DicParamIndexUtils();
		//判断输入的域名是否为空
		if(StrTool.isBlankStr(paramEName)||StrTool.isBlankStr(paramCName)){
			return XmlCodeParser.formatResultTips(false, AppCode.PARAM_NOT_NULL);
		}
		DicParamIndex dicIndex = new DicParamIndex() ;
		dicIndex.setParamCName(paramCName);
		dicIndex.setParamEName(paramEName);
		dicIndex.setParamType(0);
		//添加进数据库返回影响行数
		int ret = dicParamIndexUtils.addSysParamindex(dicIndex);
		String code = dicParamIndexUtils.getErrCode();
		return XmlCodeParser.formatResultTips(ret>0, code);
	}

	/**
	 * 修改数据字典表
	 * @param req
	 * @param resp
	 * @param p={"id":id,"paramCName":"paramCName","paramEName":"paramEName"}
	 * @param sid
	 * @return
	 */
	public String uptDicParamIndex(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int id = JsonTool.getIntValue(p, "id", -1);
		String paramCName = JsonTool.getStrValue(p, "paramCName", "");
		String paramEName = JsonTool.getStrValue(p, "paramEName", "");
		if(StrTool.isBlankStr(paramEName)||StrTool.isBlankStr(paramCName)||id==0){
			return XmlCodeParser.formatResultTips(false, AppCode.PARAM_NOT_NULL);
		}
		DicParamIndexUtils dicParamIndexUtils = new DicParamIndexUtils();
		DicParamIndex dicIndex = new DicParamIndex() ;
		dicIndex.setId(id);
		dicIndex.setParamCName(paramCName);
		dicIndex.setParamEName(paramEName);
		int ret =dicParamIndexUtils.updateParamIndex(dicIndex);
		String code = dicParamIndexUtils.getErrCode();
		return XmlCodeParser.formatResultTips(ret>0, code);
	}

	/**
	 * 根据id与paramEName删除对应的数据字典表
	 * @param req
	 * @param resp
	 * @param p={"id":id,"paramEName":"paramEName"}
	 * @param sid
	 * @return
	 */
	public String deletDicParamIndex(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int id = JsonTool.getIntValue(p, "id", -1);
		String paramEName = JsonTool.getStrValue(p, "paramEName", "");
		DicParamIndexUtils dicParamIndexUtils = new DicParamIndexUtils();
		//删除数据
		int ret = dicParamIndexUtils.delSysParamindex(id,paramEName);
		String code = dicParamIndexUtils.getErrCode();

		//删除子列表
		if(ret>0  && code.equals(SysErr.CODE_SUCCESS)){
			DicParamItemUtils dicParamItemUtils = new DicParamItemUtils();
			 ret = dicParamItemUtils.delSysParamitem(paramEName) ;
			 code = dicParamItemUtils.getErrCode();
			 return XmlCodeParser.formatResultTips(ret>0, code);
		}
		return XmlCodeParser.formatResultTips(ret>0, code);
	}

	/**
	 * 数据字典子列表查询
	 * @param req
	 * @param resp
	 * @param p={"paramId":paramId}
	 * @param sid
	 * @return
	 */
	public String getListDicParamItem(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String paramId = JsonTool.getStrValue(p, "paramId", "");
		DicParamItemUtils dicParamItemUtils = new DicParamItemUtils();
		//查询数据
		List<DicParamItem> dicParamItemList = dicParamItemUtils.getDicParamItemList(paramId, null);
		String code = dicParamItemUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JsonTool.listBeanToJson(dicParamItemList));
	}

	/**
	 * 数据字典子列表添加
	 * @param req
	 * @param resp
	 * @param p={"paramId":paramId,"value":"value","remark":"remark","description":"description"}
	 * @param sid
	 * @return
	 */
	public String addDicParamItem(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String paramId = JsonTool.getStrValue(p, "paramId", "");
		String value = JsonTool.getStrValue(p, "value", "");
		String description = JsonTool.getStrValue(p, "description", "");
		String remark = JsonTool.getStrValue(p, "remark", "");
		if(StrTool.isBlankStr(paramId)||StrTool.isBlankStr(value)||StrTool.isBlankStr(description)){
			return XmlCodeParser.formatResultTips(false,AppCode.PARAM_NOT_NULL);
		}
		DicParamItemUtils dicParamItemUtils = new DicParamItemUtils();
		//数据重复
		DicParamItem dicParamItem = new DicParamItem();
		dicParamItem.setParamId(paramId);
		dicParamItem.setValue(value);
		dicParamItem.setDescription(description);
		dicParamItem.setRemark(remark);
		//查询数据
		int ret = dicParamItemUtils.addSysParamitem(dicParamItem);
		String code = dicParamItemUtils.getErrCode();
		return XmlCodeParser.formatResultTips(ret>0, code);
	}

	/**
	 * 删除数据字典子列表
	 * @param req
	 * @param resp
	 * @param p={"id":id,"paramId":"paramId","value":"value"}
	 * @param sid
	 * @return
	 */
	public String delDicParamItemById(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int id = JsonTool.getIntValue(p, "id", -1);
		String paramId = JsonTool.getStrValue(p, "paramId", "");
		String value = JsonTool.getStrValue(p, "value", "");
		if(id==0||StrTool.isBlankStr(paramId)||StrTool.isBlankStr(value)){
			return XmlCodeParser.formatResultTips(false, AppCode.PARAM_NOT_NULL);
		}

		DicParamItemUtils dicParamItemUtils = new DicParamItemUtils();
		int ret = dicParamItemUtils.delSysParamitem(id,paramId,value);
		String code = dicParamItemUtils.getErrCode();
		return XmlCodeParser.formatResultTips(ret>0, code);
	}

	/**
	 * 数据字典子列表修改
	 * @param req
	 * @param resp
	 * @param p={"id":id"value":"value"}
	 * @param sid
	 * @return
	 */
	public String uptDicParamItem(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int id = JsonTool.getIntValue(p, "id", -1);
		String description = JsonTool.getStrValue(p, "description", "");
		String remark = JsonTool.getStrValue(p, "remark", "");
		DicParamItemUtils dicParamItemUtils = new DicParamItemUtils();
		//查询数据
		int ret = dicParamItemUtils.updateParamitem(id, description,remark);
		String code = dicParamItemUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		return XmlCodeParser.formatResultTips(ret>0, code);
	}

	/**
	 * 系统配置列表查询
	 * @return
	 */
	public String getDicParam(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		DicParamUtils dicParamUtils = new DicParamUtils();
		List<DicParam> dicParamList =dicParamUtils.getListDicParam(1);
		String code = dicParamUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}

		DicParamItemUtils dicParamItemUtils = new DicParamItemUtils();
		List<DicParamItem> dicParamItemListTemplate = dicParamItemUtils.getListSysParamItemByEName("template", 0);
		String code2 = dicParamItemUtils.getErrCode();
		if (!code2.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code2);
		}

		DicVipgradeUtils dicVipgradeUtils = new DicVipgradeUtils();
		List<DicVipgrade> querySysVipgrade =  dicVipgradeUtils.querySysVipgrade();
		String code3 = dicParamItemUtils.getErrCode();
		if (!code3.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code3);
		}

		String data = String.format("{\"dicParamList\":%s,\"querySysVipgrade\":%s,\"dicParamItemListTemplate\":%s}", JsonTool.listBeanToJson(dicParamList),JsonTool.listBeanToJson(querySysVipgrade),JsonTool.listBeanToJson(dicParamItemListTemplate));
		return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, data);
	}

	/**
	 * 系统配置列表保存
	 * @param req
	 * @param resp
	 * @param p={"id":id,"paramName":"paramName","paramValue":"paramValue",paramType}
	 * @param sid
	 * @return
	 */
	public String uptDicParam(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		List<DicParam> dicParamList = JsonTool.jsonToListBean(p, DicParam.class);
		DicParamUtils dicParamUtils = new DicParamUtils();
		//查询数据
		dicParamUtils.uptListDicParam(dicParamList,1);
		String code = dicParamUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		return XmlCodeParser.formatResultTips(true, code);
	}

	/**
	 * 接口设置查询
	 * @param req
	 * @param resp
	 * @param p
	 * @param sid
	 * @return
	 */
	public String getDicParamApi(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String startFrom = JsonTool.getStrValue(p, "startFrom", "");

		DicParamUtils dicParamUtils = new DicParamUtils();
		//查询数据
		List<DicParam> dicParamList = dicParamUtils.getListDicParam(2, startFrom);
		String code = dicParamUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		if (null != dicParamList) {
			dicParamList.forEach(item->{
				if (!StrTool.isBlankStr(item.getParamValue()) && EncryptHelper.encryptSysParam.contains(item.getParamName())) {
					item.setParamValue(EncryptUtils.encodeSession(item.getParamValue()));
				}
				if(DicParamEnum.SYS_WINIIS_CERT_MODE.equals(item.getParamName())) {
					item.setParamValue(EncryptUtils.encodeSession(LicenseUtils.getLicense()));
				}
			});
		}
		return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JsonTool.listBeanToJson(dicParamList));
	}

	/**
	 * 接口设置保存
	 * @param req
	 * @param resp
	 * @param p=[{id:id,"paramName": "paramName","paramValue": "paramValue","paramType": paramType}]
	 * @param sid
	 * @return
	 */
	public String uptDicParamApi(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String requestString = req.getParameter("p");
		List<DicParam> dicParamList = JsonTool.jsonToListBean(requestString, DicParam.class);
		DicParamUtils dicParamUtils = new DicParamUtils();
		if (null != dicParamList) {
			dicParamList.forEach(item->{
				if (EncryptHelper.encryptSysParam.contains(item.getParamName())) {
					if (!StrTool.isBlankStr(item.getParamValue())) {
						item.setParamValue(EncryptUtils.decodeSession(item.getParamValue()));
					}
				}
			});
		}
		//查询数据
		int ret =dicParamUtils.addListDicParam(dicParamList, 2);
		String code = dicParamUtils.getErrCode();
		return XmlCodeParser.formatResultTips(ret>0, code);
	}

	/**
	 * 支付和通知参数设置查询
	 * @param req
	 * @param resp
	 * @param p
	 * @param sid
	 * @return
	 */
	public String getDicParamBaseConfig(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String startFrom = JsonTool.getStrValue(p, "startFrom", "");

		DicParamUtils dicParamUtils = new DicParamUtils();
		//查询数据
		List<DicParam> dicParamList = dicParamUtils.getListDicParam(3, startFrom);
		String code = dicParamUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		if (null != dicParamList) {
			dicParamList.forEach(item->{
				if (!StrTool.isBlankStr(item.getParamValue()) && EncryptHelper.encryptSysParam.contains(item.getParamName())) {
					item.setParamValue(EncryptUtils.encodeSession(item.getParamValue()));
				}
				if(DicParamEnum.SYS_WINIIS_CERT_MODE.equals(item.getParamName())) {
					item.setParamValue(EncryptUtils.encodeSession(LicenseUtils.getLicense()));
				}
			});
		}
		return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JsonTool.listBeanToJson(dicParamList));
	}

	/**
	 * 支付和通知参数设置保存
	 * @param req
	 * @param resp
	 * @param p=[{id:id,"paramName": "paramName","paramValue": "paramValue","paramType": paramType}]
	 * @param sid
	 * @return
	 */
	public String uptDicParamBaseConfig(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String requestString = req.getParameter("p");
		List<DicParam> dicParamList = JsonTool.jsonToListBean(requestString, DicParam.class);
		DicParamUtils dicParamUtils = new DicParamUtils();
		if (null != dicParamList) {
			dicParamList.forEach(item->{
				if (EncryptHelper.encryptSysParam.contains(item.getParamName())) {
					if (!StrTool.isBlankStr(item.getParamValue())) {
						item.setParamValue(EncryptUtils.decodeSession(item.getParamValue()));
					}
				}
			});
		}
		//查询数据
		int ret =dicParamUtils.addListDicParam(dicParamList, 3);
		String code = dicParamUtils.getErrCode();
		return XmlCodeParser.formatResultTips(ret>0, code);
	}

	/**
	 * 根据EName查找子列表数据
	 * @param req
	 * @param resp
	 * @param p={"paramEName":"paramEName"}
	 * @param sid
	 * @return
	 */
	public String getListParamItemByEName(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		//json获取paramEName
		String paramEName = JsonTool.getStrValue(p, "paramEName", "");
		String value = JsonTool.getStrValue(p, "value", "");
		//新建一个DicParamItemUtils
		DicParamItemUtils dicParamItemUtils = new DicParamItemUtils();
		//调用getListSysParamItemByEName方法查询数据
		List<DicParamItem> getListParamItemByEName = dicParamItemUtils.getListSysParamItemByEName(paramEName, 0, value);
		//获取错误代码
		String code = dicParamItemUtils.getErrCode();
		//如果查询数据库出错，则抛出错误
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JsonTool.listBeanToJson(getListParamItemByEName));
	}

	/**
	 * 根据name与type查询param的对象
	 * @param req
	 * @param resp
	 * @param p={"paramName":"paramName","paramType":paramType}
	 * @param sid
	 * @return
	 */
	public String getParamByNameAndType(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String paramName = JsonTool.getStrValue(p, "paramName", "");
		int paramType = JsonTool.getIntValue(p, "paramType", -1);
		DicParamUtils dicParamUtils = new DicParamUtils();
		DicParam dicParam = dicParamUtils.getParamByNameAndType(paramName, paramType);
		String code = dicParamUtils.getErrCode();
		//判断code是否承购，不成功返回错误
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		if (null == dicParam) {
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_DATA_NOT_FOUND);
		}
		return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JsonTool.beanToJson(dicParam));
	}

	/**
	 * 根据数据值查询数据字典
	 * @param req
	 * @param resp
	 * @param p={}
	 * @param sid
	 * @return
	 */
	public String getSysParamItemByValue(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String value = JsonTool.getStrValue(p, "value", "");
		DicParamItemUtils dicParamItemUtils = new DicParamItemUtils();
		DicParamItem dpi = dicParamItemUtils.getSysParamItemByValue(value);
		String code = dicParamItemUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		if (null == dpi) {
			return  XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
		}
		return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JsonTool.beanToJson(dpi));
	}
}
